package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.ConsumeListEntity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;

public class ItemMTConsumeAdapter extends BaseQuickAdapter<ConsumeListEntity.ConsumeListDTO, BaseViewHolder> {

    public ItemMTConsumeAdapter(@Nullable ArrayList<ConsumeListEntity.ConsumeListDTO> data) {
        super(R.layout.item_consume, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ConsumeListEntity.ConsumeListDTO item) {
        helper.setText(R.id.tvMaterialName,item.getMaterialname());
        helper.setText(R.id.tvMaterialLot,item.getMlotid());
        helper.setText(R.id.tvMaterialDesc,item.getMaterialtype());
        helper.setText(R.id.etConsume,item.getConsume());
        FreeUI_ClearEditText etMaterialLot = helper.getView(R.id.etConsume);
        setEditListener(etMaterialLot,item);
    }



    private void setEditListener(EditText editText, ConsumeListEntity.ConsumeListDTO item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    item.setConsume(s.toString());
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

}
